      SUBROUTINE SOUT(SA,OMEG,TIME,IPLOT,IPLTPE,PHI)
C
C
C     SUBROUTINE SOUT DETERMINES EULER ANGLES AND RATES WRT
C     INERTIAL FRAME FOR SPINNING SATELLITES .
C     IT CALLS ARANGE TO EXTRACT ELEMENT DATA ; DMOMNT TO CALCULATE
C     ELEMENT BENDING MOMENTS AND TENSIONS ; AND WRTPLT TO WRITE
C     TEMPORARY DATA SET FOR PLOTS .
C     MODIFICATION AS OF SEPT. 1977.
C
C
        IMPLICIT REAL*8 (A-H,O-Z)
C
      COMMON/CANTNA/ A(10,3),ADOT(10,3),B(10,3),BDOT(10,3),DIN(10,3),
     .               DINDOT(10,3),DOUT(10,3),DOUTDT(10,3)
C
      COMMON/CONSTS/ PI,TWOPI,RADIAN
C
      COMMON/DMMNT1/ZKBM(6),EMAK(10),EMBK(10),DUMM1(6),
     *    ITORK,IBENDM,ITENSE,ITNS1
C
      COMMON/HAMOUT/HAMILT,IHAMLT
C
      COMMON/HOUTPT/IHCALC,IHREF,IHFLAG
C
      COMMON/HVCOUT/HBODY(3),HINERT(3),HMAG
C
      COMMON/IPOOL1/ IGRAV,IDAMP,IK,K1,ITIM,IAB,IAPS,IBB,IBPS,NK(10),
     .               LK(10),LLK(10)
C
      COMMON/NUTANG/ANUT
C
      COMMON/OUTTWO/ SOLILL,EPSERR
C
      COMMON/RPOOL2/ PO,SD(3),DAN(3,10),DBN(3,10),CFMT(3,3),DIY1(3),
     .               SD1(3),DT1,P1,AERO,DTO1,YIZK(3),PO1
C
      COMMON/SUNANG/ DSL2
C
      COMMON/VECTRS/ XSAT(3),XSATDT(3),AD(3)
C
      COMMON/XIN1/ PSI1,THET1,PHI1,ETTA,ZETTA,ITEST
C
C
      DIMENSION BS(3,3),BT(3,3),         OM(3),OMEG(3),SA(3,3)
C
      DATA THEOLD,PSIOLD,PHIOLD/ 3*0.D0/
C
      JTEST=1
      ITEST=1
      RADIUS=0.D0
      DO 4 I=1,3
    4 RADIUS=RADIUS + XSAT(I)*XSAT(I)
      RADIUS=DSQRT(RADIUS)
C
      DO 7 I=1,3
    7 BS(3,I)=XSAT(I)/RADIUS
C
      YZ=XSAT(2)*XSATDT(3) - XSAT(3)*XSATDT(2)
      ZX=XSAT(3)*XSATDT(1) - XSAT(1)*XSATDT(3)
      XY=XSAT(1)*XSATDT(2) - XSAT(2)*XSATDT(1)
C
      RXVMAG=DSQRT(YZ*YZ + ZX*ZX + XY*XY)
C
      BS(2,1)=YZ/RXVMAG
      BS(2,2)=ZX/RXVMAG
      BS(2,3)=XY/RXVMAG
C
      BS(1,1)=BS(2,2)*BS(3,3)-BS(2,3)*BS(3,2)
      BS(1,2)=BS(2,3)*BS(3,1)-BS(2,1)*BS(3,3)
      BS(1,3)=BS(2,1)*BS(3,2)-BS(2,2)*BS(3,1)
C
      DO 9 I=1,3
      DO 9 L=1,3
    9 BT(I,L)=BS(L,I)
      SDLV2=BT(1,2)*SD(1)+BT(2,2)*SD(2)+BT(3,2)*SD(3)
      DELSLV=DARCOS(-1.0D0*SDLV2)
      DSL2=DELSLV/RADIAN
C
      IF(SA(3,1).EQ.0.D0.AND.SA(3,2).EQ.0.D0) GO TO 10
C
      W=SA(3,3)
      THET1=DARCOS(W)
      IF(DABS(THET1/RADIAN).LT.1.D0) GO TO 60
      IF(THET1.EQ.0.D0) GO TO 10
      SN=DSIN(THET1)
      TEST1=SA(3,2)/SN
      W=SA(3,1)/SN
      PHI1=DARSIN(W)
      IF(TEST1.LT.0.D0) PHI1=-DARSIN(W) + PI
      TEST2=-SA(2,3)/SN
      W=SA(1,3)/SN
      PSI1=DARSIN(W)
      IF(TEST2.LT.0.D0) PSI1=-DARSIN(W) + PI
      THET1=THET1/RADIAN
      PHI1=PHI1/RADIAN
      PSI1=PSI1/RADIAN
      THEOLD=THET1
      PHIOLD=PHI1
      PSIOLD=PSI1
C
      GO TO 70
   10 IF(SA(3,3)-1.D0) 20,40,20
   20 IF(SA(3,3)+1.D0) 60,30,60
   30 THET1=180.D0
      THEOLD=THET1
      GO TO 60
   40 THET1=0.D0
      THEOLD=THET1
   60 ITEST=0
   70 X=ETTA*RADIAN
      Y=ZETTA*RADIAN
      W=SA(1,3)*DSIN(X)*DCOS(Y) + SA(2,3)*DSIN(X)*DSIN(Y)
     .                          + SA(3,3)*DCOS(X)
      EPSERR=DARCOS(W)/RADIAN
      THET1=THEOLD
      PSI1=PSIOLD
      PHI1=PHIOLD
      DO 80 I=1,3
   80 OM(I)=OMEG(I)/RADIAN
      DUM=0.0D0
      PHILD=0.0D0
      IF(IDAMP.NE.0) PHILD=PHI/RADIAN
      ANUT=0.0D0
      IF(IHCALC.NE.0) ANUT=DARCOS(HBODY(3)/HMAG)/RADIAN
C
C     CALL ARANGE
C
      IF(ITENSE.NE.0.OR.IBENDM.NE.0) CALL DMOMNT
C
      IF(IHAMLT.NE.0) CALL ENERGY(1,JTEST)
C
      IF(IPLOT.NE.0) CALL WRTPLT(TIME,PSI1,THET1,PHI1,OM,PHILD
     1                          ,ANUT,EPSERR,DUM,1)
C
      RETURN
      END
